﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace SerialPort2Socket
{
    public static class LogHelper
    {
        private static readonly log4net.ILog _logger =
            log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static LogHelper()
        {
            var processName = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName;
            string configFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
            if (!File.Exists(configFileName))
                throw new Exception("log4net config file does not exist.");
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(configFileName));
        }

        public static void Error(object message)
        {
            Error(message, new object[0]);
        }

        public static void Error(object message, params object[] args)
        {
            string msg = message.ToString();
            if (args != null && args.Length > 0)
                msg = string.Format(msg, args);
            _logger.Error(msg);
            Console.WriteLine(msg);
        }

        public static void Error(object message, Exception ex)
        {
            _logger.Error(message, ex);
            Console.WriteLine(message + Environment.NewLine + ex.ToString());
        }

        public static void Info(object message)
        {
            Info(message, new object[0]);
        }

        public static void Info(object message, params object[] args)
        {
            string msg = message.ToString();
            if (args != null && args.Length > 0)
                msg = string.Format(msg, args);
            _logger.Info(msg);
            Console.WriteLine(msg);
        }

        public static void Info(object message, Exception ex)
        {
            _logger.Info(message, ex);
            Console.WriteLine(message + Environment.NewLine + ex.ToString());
        }

        public static void Debug(object message)
        {
            Debug(message, new object[0]);
        }

        public static void Debug(object message, params object[] args)
        {
            string msg = message.ToString();
            if (args != null && args.Length > 0)
                msg = string.Format(msg, args);
            _logger.Debug(msg);
            Console.WriteLine(msg);
        }

        public static void Debug(object message, Exception ex)
        {
            _logger.Debug(message, ex);
            Console.WriteLine(message + Environment.NewLine + ex.ToString());
        }
    }
}
